<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
	"http://www.w3.org/TR/REC-html401/loose.dtd">
<html lang="en">
<head>
<META http-equiv="Content-Type" content="text/html;charset=EUC-JP">
<META http-equiv="Content-Style-Type" content="text/css">
<META http-equiv="Content-Script-Type" content="text/javascript">
<META name="generator" content="SmartDoc 1.0">
<META NAME="author" CONTENT="Naoyuki Tamura" lang="en">
<META NAME="date" CONTENT="Oct. 5, 2004" lang="en">
<title>Cream version 1.2 Programmers Guide</title>
<style type="text/css">
<!--
/*
    article.css

    since   : Nov. 21, 1998
    version : Jan. 29, 2002
    author  : ASAMI, Tomoharu (asami@XMLSmartDoc.org)
*/

A:link {
  color: green;
}

A:visited {
  color: black;
}

A:hover{
  color: gold;
};

body {
  background: white;
  font-family: times, serif;
}

h1 {
}

h1.title {
  color: black;
  background: gold;
  text-align: center;
  border-style: outset;
  border-color: yellow;
  padding: 10pt;
  margin-left: 60pt;
  margin-right: 60pt;
}

h1.part {
  font-weight: bold;
  color: black;
  background: gold;
  padding: 20pt;
}

h1.chapter {
  color: tan;
  background: black;
  text-align: left;
  padding: 5pt;
}

h2 {
  color: bisque;
  background: #666666;
  padding: 3pt;
}

h3 {
  color: black;
  background: #aaaaaa;
  padding: 2pt;
  margin-right: 100pt;
}

h4 {
  color: black;
  background: #eeeeee;
  padding: 1pt;
  margin-right: 200pt;
}

dfn {
  font-weight: bold;
  font-style: normal;
}

code {
  font-family: helvetica, sans-serif;
  font-weight: bold;
}

li p {
  text-indent: 0pt;
}

dt {
  font-weight: bold;
}

th p {
  text-indent: 0pt;
}

td p {
  text-indent: 0pt;
}

div.table {
  text-align: center;
}

table.data {
  margin-top: 10pt;
  margin-bottom: 10pt;
  background-color: green;
}

table.data caption {
  background-color: khaki;
  font-weight: bold;
}

table.data th {
  color: yellow;
  background-color: lightseagreen;
}

table.data td {
  background-color: antiquewhite;
}

table.data td.key {
  background-color: salmon;
  font-weight: bold;
}


table.menu {
  margin-top: 10pt;
  margin-bottom: 10pt;
  text-align: center;
}

table.menu caption {
  background-color: khaki;
  font-weight: bold;
}

table.menu th {
  color: yellow;
  background-color: lightseagreen;
}

table.menu td {
  color: Maroon;
  background-color: antiquewhite;
  font-weight: bold;
  padding: 10pt;
}

table.menu td.key {
  font-size: x-large;
  color: RoyalBlue;
  background-color: LightGreen;
  font-weight: bold;
  padding: 10pt;
}

table.menu td.key A:link {
  color: RoyalBlue;
}

table.menu td.key A:visited {
  color: RoyalBlue;
}

table.navigator {
}

table.navigator td {
  text-align: center;
  font-weight: bold;
}

table.languages {
  margin-bottom: 0pt;
  margin-top: 0pt;
  font-size: x-small;
}

table.languages td.current {
}

table.languages td.other {
  background-color: Orange;
}

p {
  margin: 0pt;
  text-indent: 1em;
}

p ja {
  margin: 0pt;
  text-indent: 1em;
}

p.first {
  margin: 0pt;
  text-indent: 0em;
}

p.first_ja {
  margin: 0pt;
  text-indent: 1em;
}

div.title {
  text-align: center;
}

div.toc {
  background-color: cornsilk;
  padding: 10pt;
  border-style: inset;
  border-color: cornsilk;
  margin-top: 10pt;
  margin-bottom: 30pt;
}

div.titletoc {
  background-color: cornsilk;
  padding: 10pt;
  border-style: inset;
  border-color: cornsilk;
  margin-top: 10pt;
  margin-bottom: 30pt;
}

div.date {
  text-align: center;
}

div.org {
  text-align: center;
}

div.author {
  text-align: center;
  font-weight: bold;
}

div.email {
  text-align: center;
}

div.hp {
  text-align: center;
}

div.summary {
  color: maroon;
  background: darkkhaki;
  border-style: inset;
  border-color: khaki;
  margin-top: 20pt;
  margin-bottom: 20pt;
  margin-left: 30pt;
  margin-right: 30pt;
  padding: 5pt;
}

div.comment {
  color: red;
  background: yellow;
}

span.comment {
  color: red;
  background: yellow;
}

div.navigator {
  background: khaki;
  border-top: 1pt solid black;
  border-bottom: 1pt solid black;
  text-align: center;
  margin-top: 10pt;
  margin-bottom: 10pt;
}

div.navigatortop {
  background: khaki;
  border-top: 1pt solid black;
  border-bottom: 1pt solid black;
  text-align: center;
  margin-bottom: 10pt;
}

div.navigatorbottom {
  background: khaki;
  border-top: 1pt solid black;
  border-bottom: 1pt solid black;
  text-align: center;
  margin-top: 10pt;
}

div.languages {
  text-align: right;
}

div.footnote {
  background: antiquewhite;
}

div.fyi {
  color: maroon;
  background: peachpuff;
  padding: 5pt;
  margin-top: 10pt;
  margin-bottom: 10pt;
}

div.fyi div.title {
  background: antiquewhite;
  font-weight: bold;
  padding: 5pt;
  margin-top: 5pt;
  margin-bottom: 5pt;
}

div.caption {
  background: lavender;
  font-weight: bold;
  padding: 5pd;
  width: 300pt;
  margin-top: 10pt;
}

div.result {
  background: ivory;
  border-style: inset;
  border-color: gold;
  padding: 10pt;
  margin-top: 10pt;
  margin-bottom: 10pt;
}

span.caption {
  background: lavender;
  font-weight: bold;
}

pre.program {
  background-color: gainsboro;
  font-family: courier, monospace;
  padding: 5pt;
  margin-right: 30pt;
}

pre.program em {
  color: red;
  font-style: normal;
}

pre.program strong {
  color: red;
  background-color: yellow;
  font-style: normal;
}

pre.program span.keyword {
  color: Blue;
  font-style: normal;
}

pre.program span.primitive {
  color: Green;
  font-style: normal;
}

pre.program span.comment {
  color: Red;
  background-color: gainsboro;
  font-style: normal;
}

pre.program span.quote {
  color: OrangeRed;
  font-style: normal;
}

var {
  color: green;
  font-style: italic;
}

tt {
  font-family: courier, monospace;
}

code {
  font-family: helvetica, sans-serif;
  font-weight: bold;
}

pre.console {
  background-color: black;
  color: white;
  font-family: courier, monospace;
  padding: 5pt;
  margin-right: 30pt;
}

pre.console kbd {
  font-family: courier, monospace;
  color: yellow;
}

span.toggle {
  font-weight: bold;
}

div.figure {
  text-align: center;
}

img.figure {
  margin: 10pt;
}

div.equation {
  margin-top: 1em;
  margin-right: 30pt;
  margin-bottom: 1em;
  padding-top: 1em;
  padding-left: 4em;
  padding-bottom: 1em;
  background: powderblue;
}

div.equation table {
}

div.equation table td.padding {
  width:2em
}

td.equationNumber {
  width: 4em;
  text-align: right;
}
-->
</style>
<script type="text/javascript">
<!--
/*
 * HTML4JavaScript.js
 *
 * @since   Nov. 17, 1998
 * @version Oct. 22, 2001
 * @author  ASAMI, Tomoharu (asami@zeomtech.com)
 */
var backupColor=null;
var backupBGColor=null;
var backupFontStyle=null;
var backupIDColor=null;
var backupIDBGColor=null;
var backupIDFontStyle=null;
var backupIDBorderStyle=null;

var backupOnKeyDown=null;

function normal(element) {
  if (document.all) {
    if (backupColor != null) {
      element.style.color=backupColor;
      backupColor=null;
    }
    if (backupBGColor != null) {
      element.style.background=backupBGColor;
      backupBGColor=null;
    }
    if (backupFontStyle != null) {
      element.style.fontStyle=backupFontStyle;
      backupFontStyle=null;
    }
    var id = null;
    if (element.href != null) {
      id = element.href;
      id = id.substring(id.indexOf("#") + 1);
    }
    if (id != null) {
      var target = document.all.item(id, 0);
      if (backupIDColor != null) {
        target.style.color=backupIDColor;
        backupIDColor=null;
      }
      if (backupIDBGColor != null) {
        target.style.background=backupIDBGColor;
        backupIDBGColor=null;
      }
      if (backupIDFontStyle != null) {
        target.style.fontStyle=backupIDFontStyle;
        backupIDFontStyle=null;
      }
      if (backupIDBorderStyle != null) {
        target.style.borderStyle=backupIDBorderStyle;
        backupIDBorderStyle=null;
      }
    }
  }
}

function hilight(element) {
  if (document.all) {
    backupBGColor=element.style.background;
    element.style.background="gold";
  }
}

function hilightString(element, color) {
  if (color == null) {
    color="darkolivegreen";
  }
  if (document.all) {
    backupColor=element.style.color;
    backupFontStyle=element.style.fontStyle;
    element.style.color=color;
  }
}

function hilightSelflink(element, color) {
  if (color == null) {
    color="darkolivegreen";
  }
  if (document.all) {
    backupColor=element.style.color;
    backupBGColor=element.style.background;
    backupFontStyle=element.style.fontStyle;
    element.style.color=color;
    var id = null;
    if (element.href != null) {
      id = element.href;
      id = id.substring(id.indexOf("#") + 1);
    }
    if (id != null) {
      var target = document.all.item(id, 0);
      backupIDColor=target.style.color;
//      backupIDBGColor=target.style.background;
//      backupIDFontStyle=target.style.fontStyle;
      backupIDBorderStyle=target.style.borderStyle;
      target.style.color="red";
      target.style.borderStyle="outset";
    }
  }
}

function hilightHyperlink(element, color) {
  if (color == null) {
    color="darkorange";
  }
  if (document.all) {
    backupColor=element.style.color;
    backupBGColor=element.style.background;
    backupFontStyle=element.style.fontStyle;
    element.style.color=color;
  }
}

function hilightExternallink(element, color) {
  if (color == null) {
    color="purple";
  }
  if (document.all) {
    backupColor=element.style.color;
    backupBGColor=element.style.background;
    backupFontStyle=element.style.fontStyle;
    element.style.color=color;
  }
}

function toggleChildren(node) {
  if (document.all) {
    for (i = 0;i < node.children.length;i++) {
      var child = node.children[i];
      if (child.className == "dynamic") {
        if (child.style.display == "") {
            child.style.display = "none";
        } else {
            child.style.display = "";
        }
      }
    }
  }
}

function toggleDynamic(node) {
  if (document.all) {
    if (node.tagName == "LI") {
      toggleChildren(node);
    }
  }
}

function isMsie4orGreater() { 
  var ua = window.navigator.userAgent; var msie = ua.indexOf ("MSIE");
  if (msie > 0) {
    return (parseInt (ua.substring (msie+5, ua.indexOf (".", msie))) >= 4)   
      && (ua.indexOf ("MSIE 4.0b") <0);
  } else {
    return false;
  }
}

function keyEventHandler() {
  if (document.all) {
    if (event.keyCode == 13) {
      var child = document.all.item("_next_page_", 0);
      if (child != null) {
        location.href = child.href;
      }
    } else if (event.keyCode == 39) {
      var child = document.all.item("_next_page_", 0);
      if (child != null) {
        location.href = child.href;
      }
    } else if (event.keyCode = 37) {
      var child = document.all.item("_prev_page_", 0);
      if (child != null) {
        location.href = child.href;
      }
    } else {
      backupOnKeyDown();
    }
  }
}

function setOHPHandler() {
  backupOnKeyDown=document.onkeydown;
  document.onkeydown=keyEventHandler;
}
// -->
</script>
</head>
<body>
<div style="text-align:right"><a href="http://www.XMLSmartDoc.org" target="_top"><span style="color:#4F5952;background:#EE8362;font-family:sans-serif;font-size:xx-small;text-decoration:none">Powered by </span><span style="color:#058258;background:#EE8362;font-family:fantasy, sans-serif;font-size:x-small;text-decoration:none">SmartDoc</span></a></div>
<div class="languages">
<table class="languages">
<tr>
<td class="current">ENGLISH</td><td class="other"><a href="index_ja.html" onMouseOver="hilightHyperlink(this)" onMouseOut="normal(this)">JAPANESE</a></td></tr>
</table>
</div>

<h1 class="title">Cream version 1.2 Programmers Guide</h1>

<div class=date>Oct. 5, 2004</div>
<div class=author>Naoyuki Tamura</div>
<div class=email><a href="mailto:tamura@kobe-u.ac.jp" onMouseOver="hilightHyperlink(this)" onMouseOut="normal(this)">tamura@kobe-u.ac.jp</a></div>
<div class=hp><a href="http://bach.istc.kobe-u.ac.jp/cream/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">http://bach.istc.kobe-u.ac.jp/cream/</a></div>
<div class=summary>
This document describes how to write Java programs in Cream (Constraint Resolution Enhancement And Modules) class library. In other words, <em>how to make your Java creamy</em>.</div>
<h2>Table of contents</h2>

<div class="toc">
<ul>
<li> <a href="#doc1_36" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">1  Features</a>
<li> <a href="#doc1_68" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">2  Installation</a>
<li> <a href="#doc1_106" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3  Programming</a> <span class="toggle" onMouseDown="toggleDynamic(this.parentElement)" onMouseOver="hilightString(this, 'red')" onMouseOut="normal(this)">&gt;&gt;</span>
<ul class="dynamic" style="display:none"><li> <a href="#doc3_200" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.1  First Step</a>
<li> <a href="#doc3_294" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.2  Using Coroutining Facility</a>
<li> <a href="#doc3_335" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.3  Using SolutionHandler</a>
<li> <a href="#doc3_373" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.4  Searching with Timeout</a>
<li> <a href="#doc3_415" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.5  Finding Optimal Solution</a>
<li> <a href="#doc3_432" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.6  Using Local Search</a>
<li> <a href="#doc3_514" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">3.7  Using Multiple Local Search Solvers</a>
</ul>
<li> <a href="#doc1_114" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">4  API</a>
<li> <a href="#doc1_128" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">5  License</a>
<li> <a href="#doc1_157" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">6  Acknowledgement</a>
</ul>
</div>

<h2 id="doc1_36">1  Features</h2>

<p class="first">Cream is a class library helping Java programmers to develop intelligent programs
requiring constraint satisfaction or optimization on finite domains.
The followings are features of Cream.

<ul>
<li> 100% Pure Java: Whole programs are written in Java.
<li> Open source: Cream is distributed as a free software with source code. Please refer to <a href="#doc1_128" onMouseOver="hilightSelflink(this)" onMouseOut="normal(this)">License section</a> for more details about licensing issue.
<li> Natural description of constraints: Various constraints can be naturally described within Java syntax.
<li> Easy enhancements: Programmers can easily enhance/extend constraint descriptions and satisfaction algorithms.
<li> Various optimization algorithms: Various optimization algorithms are available, such as Simulated Annealing, Taboo Search, etc.</ul></p>

<h2 id="doc1_68">2  Installation</h2>

<p class="first">There is nothing special for installation.
Please <tt>unzip</tt> the <tt>zip</tt> file you can obtained from the web page,
then you are ready to 
run example programs in <tt>examples</tt> directory as follows.

<ul>
<li> on Unix-like systems:
<pre id="doc1_87" class="console">
  java -classpath .:../cream.jar FourColor
</pre>
<li> on Windows systems:
<pre id="doc1_93" class="console">
  java -classpath .;..\cream.jar FourColor
</pre></ul>


Please note that you need
<a href="http://java.sun.com/j2se/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">Java 2, Standard Edition</a> to use Cream.</p>

<h2 id="doc1_106">3  Programming</h2>


<h3 id="doc3_200">3.1  First Step</h3>

<p class="first">This section describes how to use Cream step-by-step.

Consider an old Japanese elementary school problem:
<blockquote>
<p>There are some cranes and tortoises. They are 7 in total, and their legs are 20 in total. How many cranes and tortoises are there?</p>
</blockquote>

To solve this problem in Cream, firstly
you need to create a constraint network (an instance of <tt>Network</tt> class)
consisting of variables and constraints over those variables.

<pre id="doc3_212" class="program">
    Network net = new Network();
</pre>


Secondly, please declare and create variables for numbers of 
cranes (that is, <tt>x</tt>) and tortoises (that is, <tt>y</tt>).

<pre id="doc3_221" class="program">
    IntVariable x = new IntVariable(net);
    IntVariable y = new IntVariable(net);
</pre>


These variables are added to the constraint network by the constructor.

Thirdly, please describe constraint conditions over those variables,
that is <tt>x &gt;= 0</tt>, <tt>y &gt;= 0</tt>,
<tt>x + y == 7</tt>, and <tt>2x + 4y == 20</tt>.
First two conditions can be written in Cream as follows.

<pre id="doc3_236" class="program">
    x.ge(0);
    y.ge(0);
</pre>


These constraints are also added to the same constraint network
which the variables belong.

It is possible to write them (but lengthy) as follows.

<pre id="doc3_239" class="program">
    new IntComparison(net, IntComparison.GE, x, 0);
    new IntComparison(net, IntComparison.GE, y, 0);
</pre>



The latter two conditions <tt>x + y == 7</tt> and <tt>2x + 4y == 20</tt>
can be written simply as follows.

<pre id="doc3_248" class="program">
    x.add(y).equals(7);
    x.multiply(2).add(y.multiply(4)).equals(20);
</pre>



It is possible to rewrite them as follows.

<pre id="doc3_251" class="program">
    // 7 == x + y
    new IntArith(net, IntArith.ADD, 7, x, y);
    // t1 == x * 2
    IntVariable t1 = new IntVariable(net);
    new IntArith(net, IntArith.MULTIPLY, t1, x, 2);
    // t2 == y * 4
    IntVariable t2 = new IntVariable(net);
    new IntArith(net, IntArith.MULTIPLY, t2, y, 4);
    // 20 == t1 + t2
    new IntArith(net, IntArith.ADD, 20, t1, t2);
</pre>



Now, pass the constraint network to
<tt>DefaultSolver</tt> to solve the problem by constraint propagation and backtracking.

<pre id="doc3_257" class="program">
    Solver solver = new DefaultSolver(net);
</pre>



You can get a solution from the solver as follows.

<pre id="doc3_260" class="program">
    Solution solution = solver.findFirst();
</pre>


This code only finds the first solution, but it is sufficient in this case.

To get values of the variables in the solution, <tt>getIntValue</tt> methods can be used.

<pre id="doc3_266" class="program">
    int xv = solution.getIntValue(x);
    int yv = solution.getIntValue(y);
</pre>



The following is the whole program.

<pre id="doc3_269" class="program">
/*
 * @(#)FirstStep.java
 */
import jp.ac.kobe_u.cs.cream.*;

public class FirstStep {
    public static void main(String args[]) {
	// Create a constraint network
	Network net = new Network();
	// Declare variables
	IntVariable x = new IntVariable(net);
	IntVariable y = new IntVariable(net);
	// x &gt;= 0
	x.ge(0);
	// y &gt;= 0
	y.ge(0);
	// x + y == 7
	x.add(y).equals(7);
	// 2x + 4y == 20
	x.multiply(2).add(y.multiply(4)).equals(20);
	// Solve the problem
	Solver solver = new DefaultSolver(net);
	Solution solution = solver.findFirst();
	int xv = solution.getIntValue(x);
	int yv = solution.getIntValue(y);
	System.out.println(&quot;x = &quot; + xv + &quot;, y = &quot; + yv);
    }
}
</pre>



The same program is in <tt>examples</tt> directory.
You can compile and execute it as follows.

<ul>
<li> on Unix-like systems:
<pre id="doc3_278" class="console">
  javac -classpath .:../cream.jar FirstStep.java
  java -classpath .:../cream.jar FirstStep
</pre>
<li> on Windows systems:
<pre id="doc3_284" class="console">
  javac -classpath .;..\cream.jar FirstStep.java
  java -classpath .;..\cream.jar FirstStep
</pre></ul></p>

<h3 id="doc3_294">3.2  Using Coroutining Facility</h3>

<p class="first">If you want to find all solutions, you can use 
coroutining facility or <tt>SolutionHandler</tt> interface described in the next subsection.

The previous example program can be rewritten as follows.

<pre id="doc3_303" class="program">
    Solver solver = new DefaultSolver(net);
    for (solver.start(); solver.waitNext(); solver.resume()) {
        Solution solution = solver.getSolution();
	int xv = solution.getIntValue(x);
	int yv = solution.getIntValue(y);
	System.out.println(&quot;x = &quot; + xv + &quot;, y = &quot; + yv);
    }
    solver.stop();
</pre>


The <tt>start()</tt> method starts the solver in a new thread,
and immediately returns to the caller.
The <tt>waitNext()</tt> method is used to wait the next solution.
It returns <tt>true</tt> if the next solution is found, and returns
<tt>false</tt> if there are no more solutions.
The <tt>getSolution()</tt> method returns the solution.
The solver is suspended when the solution is found,
and it resumes the execution when the <tt>resume()</tt> method is called.
The <tt>stop()</tt> method should be called so that the solver thread is disposed cleanly.

The invocation of the <tt>stop()</tt> method during the search results in the abortion of
the solver execution.</p>

<h3 id="doc3_335">3.3  Using SolutionHandler</h3>

<p class="first"><tt>SolutionHandler</tt> can be used to find all solutions.
The previous example program can be rewritten as follows.

<pre id="doc3_344" class="program">
    Solver solver = new DefaultSolver(net);
    solver.findAll(new FirstStepHandler(x, y));
</pre>


The <tt>findAll(SolutionHandler handler)</tt> invokes
<tt>solved</tt> method of the handler for each solution and at the end of the solver execution.

The following is an example implementation of the <tt>SolutionHandler</tt>.

<pre id="doc3_356" class="program">
class FirstStepHandler implements SolutionHandler {
    IntVariable x;
    IntVariable y;

    public FirstStepHandler(IntVariable x, IntVariable y) {
	this.x = x;
	this.y = y;
    }

    public synchronized void solved(Solver solver, Solution solution) {
	if (solution != null) {
	    int xv = solution.getIntValue(x);
	    int yv = solution.getIntValue(y);
	    System.out.println(&quot;x = &quot; + xv + &quot;, y = &quot; + yv);
	}
    }
}
</pre>



Another way is the use of <tt>start(SolutionHandler handler)</tt> method.
It starts the solver in a new thread, and immediately returns to the caller.
You can wait the end of the solver execution by <tt>join()</tt> method.

<pre id="doc3_365" class="program">
    Solver solver = new DefaultSolver(net);
    solver.start(new FirstStepHandler(x, y));
    /* other jobs can be performed in parallel */
    solver.join();
</pre></p>

<h3 id="doc3_373">3.4  Searching with Timeout</h3>

<p class="first">Above mentioned methods (<tt>findFirst</tt>, <tt>findAll</tt>, and <tt>start</tt>) have
optional argument for specifying timeout in milliseconds.

<ul>
<li> <tt>findFirst(long timeout)</tt>
<li> <tt>findAll(SolutionHandler handler, long timeout)</tt>
<li> <tt>start(long timeout)</tt>
<li> <tt>start(SolutionHandler handler, long timeout)</tt></ul>



The solver stops the execution when the elapsed time exceeds the given timeout value.

It is also possible to specify the timeout value for the <tt>waitNext</tt> method.</p>

<h3 id="doc3_415">3.5  Finding Optimal Solution</h3>

<p class="first">The following is an outline of a program to find the optimal solution
by complete search.

<pre id="doc3_421" class="program">
    // set the objective variable
    net.setObjective(v);

    // set the solver to find the minimal value
    Solver solve = new DefaultSolver(net, Solver.MINIMIZE);
    for (solver.start(); solver.waitNext(); solver.resume()) {
        // find the next better solution
        Solution solution = solver.getSolution();
        .....
    }
    solver.stop();

    // get the best solution
    Solution solution = solver.getBestSolution();
</pre>


The method <tt>getBestSolution</tt> always return 
the best solution up to now.</p>

<h3 id="doc3_432">3.6  Using Local Search</h3>

<p class="first">Currently, Cream can solve a problem by local search when the problem 
involves <tt>Serialized</tt> constraints.

<ul>
<li> <p><tt>Serialized(Variable[] <i>s</i>, int[] <i>d</i>)</tt></p>
<p>All intervals
  <tt>[<i>s<sub>i</sub></i>, <i>s<sub>i</sub></i>+<i>d<sub>i</sub></i>-1]</tt>
  are not overlapped each other</p>
<li> Useful for scheduling problems</ul>


The following local search algorithms are available in Cream.

<ul>
<li> <tt>LocalSearch()</tt>: Random Walk
<li> <tt>SASearch()</tt>: Simulated Annealing
<li> <tt>TabooSearch()</tt>: Taboo Search
<li> <tt>IBBSearch()</tt>: Iterative Branch and Bound</ul>



<pre id="doc3_506" class="program">
    net.setObjective(v);

    long timeout = 60000;

    Solver solve = new SASearch(net, Solver.MINIMIZE);
    for (solver.start(timeout); solver.waitNext(); solver.resume()) {
        // find the next neighbor solution
        Solution solution = solver.getSolution();
        .....
    }
    solver.stop();

    // get the best solution
    Solution solution = solver.getBestSolution();
</pre></p>

<h3 id="doc3_514">3.7  Using Multiple Local Search Solvers</h3>

<p class="first"><ul>
<li> Cream also allows to use multiple local search solvers working in parallel (multi thread).
<li> Each solver randomly post its current best solution
<li> Other solvers randomly adopt the posted solution to generate the next neighbor</ul>



<pre id="doc3_532" class="program">
    Solver solver1 = new SASearch((Network).net.clone(), Solver.MINIMIZE);
    Solver solver2 = new TabooSearch((Network).net.clone(), Solver.MINIMIZE);
    Solver[] solvers = { solver1, solver2 };
    Solver solver = new ParallelSolver(solvers);

    for (solver.start(timeout); solver.waitNext(); solver.resume()) {
        Solution solution = solver.getSolution();
        .....
    }
    solver.stop();

    Solution solution = solver.getBestSolution();
</pre></p>
<p></p>

<h2 id="doc1_114">4  API</h2>

<p class="first">See <a href="../../api/index.html" onMouseOver="hilightHyperlink(this)" onMouseOut="normal(this)">Cream API Specification</a>.</p>

<h2 id="doc1_128">5  License</h2>

<p class="first"><dl>
<dt>
Cream (Class Library for Constraint Programming in Java)
<dt>
Copyright (C) 2003, 2004 by Naoyuki Tamura (tamura@kobe-u.ac.jp)</dl>


Cream is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA</p>

<h2 id="doc1_157">6  Acknowledgement</h2>

<p class="first">Cream was developed as a part of
<a href="http://kaminari.scitec.kobe-u.ac.jp/hecs/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">HECS
(HEterogeneous Constraint Solver) system</a> which was supported in part
by 
<a href="http://www.meti.go.jp/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">METI</a> and
<a href="http://www.ipa.go.jp/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">IPA (The Information-technology Promotion Agency)</a>
under grant of
<a href="http://www.ipa.go.jp/NBP/14nendo/14mito/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">2002 Exploratory Software Project</a> and
<a href="http://www.ipa.go.jp/NBP/15nendo/15mito/" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">2003 Exploratory Software Project</a>.
Some improvement ideas of Cream are obtained through the discussion with
Mr. Kino, a project manager of IPA Exploratory Software Project
and also a developer of <a href="http://www.kprolog.com" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">K-Prolog</a> and 
<a href="http://www.isac.co.jp/ICS/ics_injava.html" target="_top" onMouseOver="hilightExternallink(this)" onMouseOut="normal(this)">ICS in Java</a>.</p>
</body>
</html>
